import { Window_Selectable } from "@/windows/Window_Selectable.js";
import { TextManager } from "@/managers/TextManager.js";
import { ConfigManager } from "@/managers/ConfigManager";
import { clamp } from "@/utils/number";

export class Window_Options extends Window_Selectable {
    constructor(width,height) {
        super(width,height,false);
        this.init(width,height);
    }

    init(width,height) {
        this._list=null;
        this.cancelHandler=null;
        super.init(width, height);
    }

    row(){return this.length();}
    viewRow(){return Math.floor(this.contentsHeight()/this.itemHeight());}
    length(){return this._list.length;}

    makeItemList(){
        this._list=[
            {
                name:TextManager.alwaysDash,
                getValue(){return ConfigManager.alwaysDash?'ON':'OFF';},
                callback(){ConfigManager.alwaysDash=!ConfigManager.alwaysDash;}
            },
            {
                name:TextManager.commandRemember,
                getValue(){return ConfigManager.commandRemember?'ON':'OFF';},
                callback(){ConfigManager.commandRemember=!ConfigManager.commandRemember;}
            },
            {
                name:TextManager.bgmVolume,
                getValue(){return `${ConfigManager.bgmVolume}%`;},
                callback(){
                    ConfigManager.bgmVolume=(ConfigManager.bgmVolume+10)%100;
                }
            },
            {
                name:TextManager.bgsVolume,
                getValue(){return `${ConfigManager.bgsVolume}%`;},
                callback(){ConfigManager.bgsVolume=(ConfigManager.bgsVolume+10)%100;}
            },
            {
                name:TextManager.meVolume,
                getValue(){return `${ConfigManager.meVolume}%`;},
                callback(){ConfigManager.meVolume=(ConfigManager.meVolume+10)%100;}
            },
            {
                name:TextManager.seVolume,
                getValue(){return `${ConfigManager.seVolume}%`;},
                callback(){ConfigManager.seVolume=(ConfigManager.seVolume+10)%100;}
            }
        ]
    }

    drawItem(index,x,y,width,height) {
        let item=this._list[index];
        if(item){
            let offset=width-120;
            this.drawText(item.name,x,y,offset,height);
            this.drawText(item.getValue(),x+offset,y,120,height,'right');
        }
    }

    refresh(){
        this.makeItemList();
        super.refresh();
    }

    setHandler(name,callback){
        if(name=='cancel'){
            this.cancelHandler=callback;
        }
    }

    onResponse(index){
        let item=this._list[index];
        if(item){
            item.callback();
            this.refreshItem(index);
        }
    }

    onCancel(){
        this.cancelHandler?.();
    }
}